class ArcObserver < ActiveRecord::Observer
	
  def after_create(arc)
    begin
      FileUtils.mkdir_p(arc.base_path) unless File.exists?(arc.base_path)
    rescue
      raise "Error creating directory #{arc.base_path}. Creation of arc #{arc.id} aborted."
    end
    
    validate_expression(arc)
    
    true
  end

  def validate_expression(arc)
    return unless arc.xslt_url

    if arc.xslt_url && arc.from_is_place 
      raise "Arc expressions are only allowed on outgoing arcs. Arc id: #{arc.id}"
    elsif arc.xslt_url
      #copy stylesheet to filesystem
      ArcExprFileGenerator.initializeArcXSLT(arc, true)
      
      #validate stylsheet
      xsl = XML::XSLT.new
      xsl.xsl = arc.xslt_path
    end
  end

  def before_destroy(arc)
    cleanup(arc)

    true
  end

  def cleanup(arc)
    begin
      FileUtils.rm_r arc.base_path, :force => :true
    rescue Exception => ex
      Merb.logger.error("Could not delete arc folder #{transition.base_path.to_s}")
    end
  end

end
